package com.oa.common.utils;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.oa.modules.sys.entity.TSysDictitemEntity;
import com.oa.modules.sys.service.TSysDictitemService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 字典工具类
 */
@Component
public class DictItemUtils implements ApplicationRunner {


    /**
     * 字典组List
     */
    public static Map<String, List<DictItemBean>> dictItemsList = new ConcurrentHashMap<>();


    /**
     * 字典组Map
     */
    public static Map<String, ConcurrentHashMap<String, String>> dictItemsMap = new ConcurrentHashMap<>();

    @Autowired
    private TSysDictitemService tSysDictitemService;

    /**
     * 初始化字典组
     *
     * @param args
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<TSysDictitemEntity> tSysDictitemEntityList = tSysDictitemService.list(new QueryWrapper<TSysDictitemEntity>().eq("`status`", "1").orderByAsc("GROUP_ID", "`ORDER`"));
        for (TSysDictitemEntity tSysDictitemEntity : tSysDictitemEntityList) {
            //List处理
            String groupId = tSysDictitemEntity.getGroupId();
            List<DictItemBean> itemList = dictItemsList.get(groupId);
            if (null == itemList) {
                itemList = new ArrayList<>();
                dictItemsList.put(groupId, itemList);
            }
            DictItemBean dictItemBean = new DictItemBean();
            dictItemBean.setDictValue(tSysDictitemEntity.getDictValue());
            dictItemBean.setDictName(tSysDictitemEntity.getDictName());
            dictItemBean.setOrder(tSysDictitemEntity.getOrder());
            itemList.add(dictItemBean);
            //MAP处理
            ConcurrentHashMap<String, String> itemMap = dictItemsMap.get(groupId);
            if (null == itemMap) {
                itemMap = new ConcurrentHashMap<>();
                dictItemsMap.put(groupId, itemMap);
            }
            itemMap.put(tSysDictitemEntity.getDictValue(), tSysDictitemEntity.getDictName());
        }
    }


    /**
     * 翻译字典组
     *
     * @param groupId
     * @param itemValue
     * @return
     */
    public static String getDictName(String groupId, String itemValue) {
        Map<String, String> dictItems = dictItemsMap.get(groupId);
        if (dictItems != null) {
            String itemName = dictItems.get(itemValue);
            if (StringUtils.isNotBlank(itemName)) {
                return itemName;
            }
            return itemValue;

        }
        return null;
    }

    /**
     * 字典实体内部类
     */
    @Data
    @ApiModel
    class DictItemBean {

        /**
         * 字典id
         */
        @ApiModelProperty(value = "字典值")
        private String dictValue;

        /**
         * 字典名称
         */
        @ApiModelProperty(value = "字典名称")
        private String dictName;

        /**
         * 排序
         */
        @ApiModelProperty(value = "排序")
        private Integer order;


    }
}
